// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129

const db = uniCloud.databaseForJQL()


module.exports = {
	_before: function() { // 通用预处理器

	},

	async cate() {
		const cat1Map = new Map();
		const cat1Temp = db.collection("rightCategories").field("name,_id").getTemp()
		const cat2Temp = db.collection("leftCategories").field("parent_id, name, _id ").getTemp()
		const cat3Temp = db.collection("threeCategories").field("parent_id, name, _id,icon.url as icurl")
			.getTemp()

		const cat1 = await db.collection(cat1Temp, cat2Temp).get()
		const cat2 = await db.collection(cat2Temp, cat3Temp).get()

		cat1.data.forEach(category => {
			category._id.leftCategories.forEach(subCategory => {
				cat1Map.set(subCategory._id, subCategory);
			});
		});

		// 遍历 cat2 的数据，将三级分类合并到 cat1 中对应的二级分类下
		cat2.data.forEach(category => {
			category._id.threeCategories.forEach(threeCategory => {
				const subCategory = cat1Map.get(threeCategory.parent_id);
				if (subCategory) {
					if (!subCategory.threeCategories) {
						subCategory.threeCategories = [];
					}
					subCategory.threeCategories.push(threeCategory);
				}
			});
		});

		return cat1.data;
	}

	/**
	 * method1方法描述
	 * @param {string} param1 参数1描述
	 * @returns {object} 返回值描述
	 */
	/* 
	method1(param1) {
		// 参数校验，如无参数则不需要
		if (!param1) {
			return {
				errCode: 'PARAM_IS_NULL',
				errMsg: '参数不能为空'
			}
		}
		// 业务逻辑
		
		// 返回结果
		return {
			param1 //请根据实际需要返回值
		}
	}
	*/
}